import 'package:flutter/material.dart';

enum SearchBarType { home, normal, homeLight }

class SearchBar extends StatefulWidget {
  final bool endabled;
  final bool hideLeft;
  final SearchBarType searchBarType;
  final String hint;
  final String defaultText;
  final void Function() leftButtonClick;
  final void Function() rightButtonClick;
  final void Function() speakClick;
  final void Function() inputBoxClick;
  final ValueChanged<String> onChanged;

  SearchBar(this.leftButtonClick, this.rightButtonClick, this.speakClick,
      this.inputBoxClick, this.onChanged,
      {this.endabled = true,
      this.hideLeft = false,
      this.searchBarType = SearchBarType.home,
      this.hint = '',
      this.defaultText = ''});

  @override
  _SearchBarState createState() => _SearchBarState();
}

class _SearchBarState extends State<SearchBar> {
  bool showClear = false;
  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.defaultText != '') {
      setState(() {
        _controller.text = widget.defaultText;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return widget.searchBarType == SearchBarType.normal
        ? _genNormalSearch
        : _genHomeSearch;
  }

  Widget get _genNormalSearch {
    return Container(
      child: Row(
        children: [
          _wrapTap(
              Container(
                padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                child: widget.hideLeft
                    ? null
                    : Icon(
                        Icons.arrow_back_ios,
                        color: Colors.grey,
                        size: 26,
                      ),
              ),
              widget.leftButtonClick),
          Expanded(flex: 1, child: _inputBox()),
          _wrapTap(
              Container(
                padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                child: Text(
                  '搜索',
                  style: TextStyle(color: Colors.blue, fontSize: 17),
                ),
              ),
              widget.rightButtonClick)
        ],
      ),
    );
  }

  Widget get _genHomeSearch {
    return Container(
      child: Row(
        children: [
          _wrapTap(
              Container(
                  padding: EdgeInsets.fromLTRB(6, 5, 5, 5),
                  child: Row(
                    children: [
                      Text(
                        '上海',
                        style: TextStyle(color: _homeFontColor(), fontSize: 14),
                      ),
                      Icon(
                        Icons.expand_more,
                        color: _homeFontColor(),
                        size: 22,
                      ),
                    ],
                  )),
              widget.leftButtonClick),
          Expanded(
            child: _inputBox(),
            flex: 1,
          ),
          _wrapTap(
              Container(
                padding: EdgeInsets.fromLTRB(10, 5, 10, 5),
                child: Icon(
                  Icons.comment,
                  color: _homeFontColor(),
                  size: 26,
                ),
              ),
              widget.rightButtonClick)
        ],
      ),
    );
  }

  _homeFontColor() {
    return widget.searchBarType == SearchBarType.homeLight
        ? Colors.black54
        : Colors.white;
  }

  _wrapTap(Widget child, void Function() callback) {
    return GestureDetector(
      onTap: () {
        print('ddddd');
        callback();
      },
      child: child,
    );
  }

  _inputBox() {
    Color inputBoxColor;
    if (widget.searchBarType == SearchBarType.home) {
      inputBoxColor = Colors.white;
    } else {
      inputBoxColor = Color(int.parse('0xffEDEDED'));
    }
    Paint paintRed = Paint();
    paintRed.color = Colors.red;

    return Container(
      alignment: Alignment.center,
      margin: EdgeInsets.only(top: 10, bottom: 10),
      decoration: BoxDecoration(
          color: Color(int.parse('0xff'+'D3D3D3')),
          borderRadius: BorderRadius.circular(
              widget.searchBarType == SearchBarType.normal ? 5 : 15)),
      height: 30,
      child: Row(
//        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.center,
        verticalDirection: VerticalDirection.down,
        children: [
          Container(
            margin: EdgeInsets.only(left: 10,right: 10),
            child: Icon(
              Icons.search,
              size: 20,
              color: widget.searchBarType == SearchBarType.normal
                  ? Color(0xffA9A9A9)
                  : Colors.blue,
            ),
          ),

          Expanded(
             flex: 1,
             child: widget.searchBarType == SearchBarType.normal
                 ? Container(

               child: TextField(

                 textAlign: TextAlign.left,
//                 textAlignVertical: TextAlignVertical.center,
                 controller: _controller,
                 onChanged: _onChanged,
                 autofocus: true,
                 style: TextStyle(
//                          background: paintRed,
                     fontSize: 18.0,
                     color: Colors.black,
                     fontWeight: FontWeight.w300),
                 decoration: InputDecoration(
//                   contentPadding: EdgeInsets.fromLTRB(1, 3, 1, 3),
//                   contentPadding: EdgeInsets.only(bottom: 0),
                   contentPadding: EdgeInsets.symmetric(vertical: 0,horizontal: 12),
                   border: InputBorder.none,
                   hintText: widget.hint,
                   hintStyle: TextStyle(fontSize: 15),
                   enabledBorder: OutlineInputBorder(
                     borderSide: BorderSide(color: Color(0xFFDCDFE6)),
                     borderRadius: BorderRadius.all(Radius.circular(4.0)),
                   ),
                   focusedBorder: OutlineInputBorder(
                     borderSide: BorderSide(color: Colors.transparent),
                     borderRadius: BorderRadius.all(Radius.circular(4.0)),
                   ),
                 ),
               ),
             )
                 : _wrapTap(
                 Container(
                   child: Text(
                     widget.defaultText,
                     style: TextStyle(fontSize: 13, color: Colors.grey),
                   ),
                 ),
                 widget.inputBoxClick)),
          Container(
            margin: EdgeInsets.only(right: 10),
            child: !showClear
              ? _wrapTap(
              Icon(
                Icons.mic,
                size: 22,
                color: widget.searchBarType == SearchBarType.normal
                    ? Colors.blue
                    : Colors.grey,
              ),
              widget.speakClick)
              : _wrapTap(
              Icon(
                Icons.clear,
                size: 22,
                color: widget.searchBarType == SearchBarType.normal
                    ? Colors.blue
                    : Colors.grey,
              ), _clickClear)
            ,)
        ],
      ),
    );
  }
  _clickClear(){
    print('点击清空操作');
    setState(() {
      _controller.clear();
    });
    _onChanged('');
  }
  _onChanged(String text) {
    if (text.length > 0) {
      setState(() {
        showClear = true;
      });
    } else {
      setState(() {
        showClear = false;
      });
    }
    widget.onChanged(text);
  }
}
